package orderCus;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import common.Cart;
import common.Customer;

public class CartEditServlet extends HttpServlet 
{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		perform(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		perform(req, resp);
	}
	private void perform(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		HttpSession session = req.getSession();
		Customer user = (Customer)session.getAttribute("user");
		if(user == null || user.equals("null")) resp.sendRedirect(req.getContextPath() +"/index.jsp");
		else
		{
			Cart cart = (Cart)session.getAttribute("cart");
			req.setCharacterEncoding("UTF-8");
			String strOrders = req.getParameter("orders");
			String strNum = req.getParameter("num");
			int orders = 0;
			int num = Integer.parseInt(strNum);
			if(strOrders != null)
			{
				orders = Integer.parseInt(strOrders); 
				cart.update(num, orders);
			}
			else
			{
				cart.delete(num);
			}
			
			session.setAttribute("cart", cart);
			RequestDispatcher dispatcher = req.getRequestDispatcher("../customer/cart.jsp");
			dispatcher.forward(req, resp);

		}
	}
}
